package com.jgybzx.control;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.alibaba.fastjson.JSON;
import com.jgybzx.entity.Staff;
import com.jgybzx.excel.CustomHandler;
import com.jgybzx.excel.Customer;
import com.jgybzx.excel.EasyExcelUtils;
import com.jgybzx.listener.StaffListener;
import com.jgybzx.service.StaffService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.alibaba.excel.EasyExcelFactory.write;

/**
 * des:
 *
 * @author jgybzx
 * @date 2021/6/30 14:34
 */
@RestController
@RequestMapping("excel")
public class ExcelController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private StaffService staffService;

    /**
     * excel下载
     *
     * @author jgybzx
     * @date 2021/6/29 17:52
     */
    @PostMapping("download")
    public void download(HttpServletResponse response, @RequestBody List<Customer> customerList) throws IOException {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String transformDate = simpleDateFormat.format(new Date());
            String fileName = "咨询人查询" + transformDate;
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 标题样式
            WriteCellStyle headWriteCellStyle = EasyExcelUtils.getHeadStyle();
            // 内容样式
            WriteCellStyle contentWriteCellStyle = EasyExcelUtils.getContentStyle();
            // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
            CustomHandler customHandler = new CustomHandler(headWriteCellStyle, contentWriteCellStyle);
            write(response.getOutputStream(), Customer.class)
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    .registerWriteHandler(customHandler)
                    .sheet("信息").doWrite(customerList);
        } catch (Exception e) {
            response.reset();
            response.setStatus(500);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<String, String>();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));

        }
    }


    /**
     * excel读取数据导入到数据库
     *
     * @param file
     * @author 郭江勇
     * @date 2021/12/16/0016 14:34
     */
    @PostMapping("import")
    public void excelImport(@Param("file") MultipartFile file) throws IOException {
        log.info("没请求到？");
        EasyExcel.read(file.getInputStream(), Staff.class, new StaffListener(staffService)).sheet().doRead();
    }

}
